package com.niguang.work.api.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.niguang.common.constant.ResultCode;
import com.niguang.common.pojo.Result;
import com.niguang.work.api.entity.WechatUser;
import com.niguang.work.api.service.WechatUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.io.IOException;

/**
 * <p>
 * 'work.v_personSite' is not BASE TABLE 前端控制器
 * </p>
 *
 * @author chenliu
 * @since 2018-07-23
 */
@RestController
@RequestMapping("/api/wechatUser/")
@Slf4j
public class WechatUserController {

    @Resource
    private WechatUserService wechatUserService;

    /**
     * 跳转M版页面
     *
     * @param request
     * @param response
     * @return
     */
    @GetMapping("toWechatPage")
    public Object toWechatPage(HttpServletRequest request, HttpServletResponse response) {
        try {
            return wechatUserService.toWechatPage(request, response);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }


    /**
     * 服务号M版绑定平台用户及微信用户
     *
     * @param wechatUser
     * @return
     */
    @PostMapping("bindUser")
    public Result bindUser(@Valid WechatUser wechatUser) {
        return wechatUserService.bindUser(wechatUser);
    }

    @GetMapping("getUser")
    public Result getUser(@NotNull @RequestParam String openId) {
        EntityWrapper<WechatUser> entityWrapper = new EntityWrapper<>();
        entityWrapper.eq(WechatUser.OPEN_ID, openId).eq(WechatUser.DEL_FLAG, 0);
        WechatUser wechatUser = wechatUserService.selectOne(entityWrapper);
        if (ObjectUtils.isEmpty(wechatUser)) {
            return Result.build(ResultCode.SUCCESS, "用户未绑定！");
        }
        return Result.build(ResultCode.SUCCESS, "用户已绑定！", wechatUser);
    }


    /**
     * 获取短信验证码
     *
     * @param userPhone
     * @return
     */
    @GetMapping("sendSms")
    public Result sendSms(@NotNull String userPhone) {
        return wechatUserService.sendSms(userPhone);
    }


    /**
     * 获取微信推送新员工入职消息
     *
     * @param pushUid
     * @return
     */
    @GetMapping("getPushNewUserMsg")
    public Result getPushNewUserMsg(String pushUid) {
        return wechatUserService.getPushNewUserMsg(pushUid);
    }

    @GetMapping("pushWeChateTitle")
    public Result pushWeChateTitle() {
        return wechatUserService.pushWeChateTitle();
    }


}

